<?php

class Sgit_Controller_Action_Helper_Profiler extends Zend_Controller_Action_Helper_Abstract
{
    protected $_profiler;
    protected $_view;
    protected $_config;
    protected $_db;



    public function init()
    {
        
        $this->t1 = microtime(true);
        $this->language();
        
    }


    public function postDispatch()
    {
        
        $view = $this->getActionController()->view;
        $view->timeServer = 'Dispatch: ' . round(microtime(true) - $this->t1, 1) . ' seg';


    }

    public function language()
    {
        
        if (Zend_Auth::getInstance()->hasIdentity())
        {
            
            //cojo idioma de configuracion usuario
            $mylocale = Zend_Auth::getInstance()->getIdentity()->locale;
            
            $mylocale = $mylocale ? $mylocale : 'es_ES';
            
            $locale = new Zend_Locale($mylocale);

            Zend_Registry::set('Zend_Locale', $locale);
            
            $cache = Zend_Registry::get('cache');
            
            
            Zend_Date::setOptions(array('cache' => $cache));
            
            Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
            
            Zend_Translate::setCache($cache);


            //Los mensajes traducidos del validate se encuentran un directorio en configs, ya vienen en el Framework
            $translator = new Zend_Translate(
                array(
                    'adapter' => 'array',
                    'content' => APPLICATION_PATH.'/configs/languages_validate',
                    'locale'  => $localeValue,
                    'scan' => Zend_Translate::LOCALE_DIRECTORY
                )
            );
            Zend_Validate_Abstract::setDefaultTranslator($translator);

   
            $cache_id="Translate_Array_Options_$mylocale";

            //quiza haya otra forma de cachear el zend_transalte en lugar de la db
            if (($content = $cache->load($cache_id)) === false)
            {
                //como la referencia del trtanalate es español si locale es español no
                if ($localeValue != 'es_ES')
                {
                    $this->_db=Zend_Registry::get('database');
                    
                    $select = $this->_db->select()->from('translation', array('es_ES', $mylocale));
                    $translateTable = $this->_db->fetchAll($select);
                    
                    foreach ($translateTable as $v)
                    {
                        $content[$v['es_ES']] = $v[$mylocale];
                    }
                    
                    $cache->save($content, $cache_id);
                }
                
            }
            


            
            //$writer = new Zend_Log_Writer_Stream(sprintf('%1s/logs/logUntranslated_%2s.txt',APPLICATION_PATH,date('dm')));
            //$loggerTxt = new Zend_Log($writer);
            
            $translate = new Zend_Translate(array(
                'adapter' => 'array', 
                'content' => $content, 
                'locale' => $mylocale,
                //'log'=>$loggerTxt,
               // 'logUntranslated'=>true
            ));
            
            Zend_Form::setDefaultTranslator($translate);
            Zend_Registry::set('Zend_Translate', $translate);
            
     
        }

    }


}
